import { deepStrictEqual, strictEqual } from 'assert';
import { render } from 'ejs';
import { Mock, mockAny } from 'lite-ts-mock';
import { ThreadBase } from 'lite-ts-thread';
import { TreePSD } from 'psd';

import { CanvasConverter as Self } from './canvas';
import { AssertServiceBase, ConverterFactoryBase, FileFactoryBase, IFile } from '../../contract';
import { enum_ } from '../../model';

describe('src/scene/service/converter/canvas.ts', () => {
    describe('.convert()', () => {
        it('ok', async () => {
            const mockAssetService = new Mock<AssertServiceBase>();
            const mockConverterFactory = new Mock<ConverterFactoryBase>();
            const mockFileFactory = new Mock<FileFactoryBase>({
                view: 'a-b'
            });
            const mockThread = new Mock<ThreadBase>();
            const mockTreePSD = new Mock<TreePSD>();
            const self = new Self(
                mockThread.actual,
                mockAssetService.actual,
                mockConverterFactory.actual,
                mockFileFactory.actual,
                mockTreePSD.actual,
            );

            mockTreePSD.expectReturn(
                r => r.get(enum_.Psd.width),
                100,
            );

            mockTreePSD.expectReturn(
                r => r.get(enum_.Psd.height),
                90
            );

            const node = {};
            Reflect.set(self, 'createNode', (arg: any) => {
                deepStrictEqual(arg, {
                    x: -50,
                    y: 45
                });
                return node;
            });

            const mockFile = new Mock<IFile>({
                name: 'name.ts'
            });
            mockFileFactory.expectReturn(
                r => r.buildFile('canvas.ts'),
                mockFile.actual
            );

            mockFile.expectReturn(
                r => r.exists(),
                false
            );

            mockFile.expected.write(
                render(Self.codeTemplate, {
                    className: 'ABCanvas',
                    isModal: false,
                })
            );

            mockAssetService.expected.refresh('name.ts');

            mockThread.expected.try(mockAny, 10, 300);

            let isCalled = false;
            Reflect.set(self, 'addChildren', (arg: any) => {
                strictEqual(arg, node);
                isCalled = true;
            });

            const res = await self.convert();
            strictEqual(res, node);
            strictEqual(isCalled, true);
        });
    });
})